package com.tcgroup.phone.base_core.framework.mvvm.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import com.tcgroup.phone.base_core.framework.mvvm.AbstractFragment;
import com.tcgroup.phone.base_core.framework.mvvm.viewmodel.BaseViewModel;

import java.util.Map;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/18.
 * 包名: com.tcgroup.phone.base_core.framework.mvvm.fragment
 * 类名: OSAndroidMvvm
 */
public abstract class BaseMvvmFragment<V extends ViewDataBinding,VM extends BaseViewModel> extends AbstractFragment {
    protected V mBinding;
    protected VM mViewModel;
    private int viewModelId;

    @Override
    public void initConentView(ViewGroup root){
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(mActivity), onBindLayout(),root,true);
        initViewModel();
        initBaseViewObservable();

    }

    private void initViewModel() {
        mViewModel = createViewModel();
        viewModelId = onBindVariableId();
        if(mBinding != null){
            mBinding.setVariable(viewModelId, mViewModel);
        }
        getLifecycle().addObserver(mViewModel);
    }
    public VM createViewModel(){
        return ViewModelProviders.of(this,onBindViewModelFactory()).get(onBindViewModel());
    }

    public abstract Class<VM> onBindViewModel();
    public abstract ViewModelProvider.Factory onBindViewModelFactory();
    public abstract void initViewObservable();
    public abstract int onBindVariableId();

    protected void initBaseViewObservable() {
        initViewObservable();
        mViewModel.getUC().getShowInitLoadViewEvent().observe(this, (Observer<Boolean>) show -> {
            if (show){
                showLoading();
            }
        });
        mViewModel.getUC().getShowNoDataViewEvent().observe(this, (Observer<Boolean>) show -> {
            if (show){
                showEmpty();
            }
        });
        mViewModel.getUC().getShowNetWorkErrViewEvent().observe(this, (Observer<Boolean>) show -> {
            if (show){
                showError();
            }
        });
        mViewModel.getUC().getStartActivityEvent().observe(this, (Observer<Map<String, Object>>) params -> {
            Class<?> clz = (Class<?>) params.get(BaseViewModel.ParameterField.CLASS);
            Bundle bundle = (Bundle) params.get(BaseViewModel.ParameterField.BUNDLE);
            startActivity(clz, bundle);
        });
        mViewModel.getUC().getFinishActivityEvent().observe(this, (Observer<Void>) v -> {
            if(mActivity != null){
                mActivity.finish();
            }
        });
        mViewModel.getUC().getOnBackPressedEvent().observe(this, (Observer<Void>) v -> {
            if(mActivity != null){
                mActivity.onBackPressed();
            }
        });

        mViewModel.getUC().getShowCurrentEventEvent().observe(this, (Observer<Boolean>) o -> {
            if (o){
                showContent();
            }
        });
    }

    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent(mActivity, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
}
